/*
 * Copyright 2008 Zoundry LLC, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */
package org.zoundry.gaedt.ui.views.actions;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.zoundry.gaedt.services.applications.IZGaeApplication;
import org.zoundry.gaedt.services.applications.IZGaeApplicationsService;
import org.zoundry.gaedt.ui.ZGaeUIImages;

 abstract class ZGaeApplicationsViewActionBase extends Action
{
    /** The table of applications. */
    private TableViewer mViewer;
    /** The application service. */
    private IZGaeApplicationsService mApplicationService;

    /**
     * C'tor.
     * 
     * @param aText
     * @param aTooltip
     * @param aImagePath
     * @param aViewer
     * @param aService
     */
    public ZGaeApplicationsViewActionBase(String aText, String aTooltip, String aImagePath, TableViewer aViewer,
            IZGaeApplicationsService aService)
    {
        setViewer(aViewer);
        setApplicationService(aService);
        setText(aText);
        setToolTipText(aTooltip);
        if (aImagePath != null)
            setImageDescriptor(ZGaeUIImages.getImageDescriptor(aImagePath));
    }
    
    /**
     * @see org.eclipse.jface.action.Action#run()
     */
    @Override
    public abstract void run();

    /**
     * @return the viewer
     */
    protected TableViewer getViewer()
    {
        return mViewer;
    }

    /**
     * @param aViewer the viewer to set
     */
    protected void setViewer(TableViewer aViewer)
    {
        mViewer = aViewer;
    }

    /**
     * @return the applicationService
     */
    protected IZGaeApplicationsService getApplicationService()
    {
        return mApplicationService;
    }

    /**
     * @param aApplicationService the applicationService to set
     */
    protected void setApplicationService(IZGaeApplicationsService aApplicationService)
    {
        mApplicationService = aApplicationService;
    }

    /**
     * Returns the {@link IZGaeApplication} that is currently selected in the table.
     */
    protected IZGaeApplication getSelectedApplication()
    {
        IStructuredSelection selection = (IStructuredSelection) getViewer().getSelection();
        if (!selection.isEmpty())
            return (IZGaeApplication) selection.getFirstElement();
        return null;
    }
}
